/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.mes.base.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.mes.base.entity.DictTreeNode;
import org.springblade.modules.mes.base.entity.MaterialGroup;
import org.springblade.modules.mes.base.mapper.MaterialGroupMapper;
import org.springblade.modules.mes.base.service.IMaterialGroupService;
import org.springblade.modules.mes.base.utils.StringUtils;
import org.springblade.modules.mes.base.vo.MaterialGroupVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 材料组 服务实现类
 *
 * @author BladeX
 * @since 2020-09-11
 */
@Service
public class MaterialGroupServiceImpl extends BaseServiceImpl<MaterialGroupMapper, MaterialGroup> implements IMaterialGroupService {

	@Resource
	MaterialGroupMapper materialGroupMapper;

	@Override
	public IPage<MaterialGroupVO> selectMaterialGroupPage(IPage<MaterialGroupVO> page, MaterialGroupVO materialGroup) {
		materialGroup.setMaterialTypes(Func.toStrList(materialGroup.getMaterialType()));
		materialGroup.setTenantId(AuthUtil.getTenantId());
		return page.setRecords(baseMapper.selectMaterialGroupPage(page, materialGroup));
	}

	@Override
	public List<MaterialGroupVO> getMaterialGroupVOList(MaterialGroupVO materialGroupVO) {
		materialGroupVO.setMaterialTypes(Func.toStrList(materialGroupVO.getMaterialType()));
		materialGroupVO.setTenantId(AuthUtil.getTenantId());
		return materialGroupMapper.getMaterialGroupVOList(materialGroupVO);
	}

	@Override
	public List<MaterialGroup> listWithDeleted(Date startTime, Date endTime) {
		return materialGroupMapper.listWithDeleted(startTime, endTime);
	}

	@Override
	public List<DictTreeNode> getTree(String code, String dictKey, String childKey) {
		List<String> dictKeys = null;
		if (StringUtils.isNotBlank(dictKey)) {
			dictKeys = Func.toStrList(dictKey);
		}
		List<String> childKeys = null;
		if (StringUtil.isNotBlank(childKey)) {
			childKeys = Func.toStrList(childKey);
		}
		return ForestNodeMerger.merge(baseMapper.getTree(code, dictKeys, childKeys));
	}

	@Override
	public List<MaterialGroup> materialGroupList() {
		return baseMapper.materialGroupList();
	}

	/**
	 * 根据物料类型获取物料组信息
	 *
	 * @param materialType
	 * @return
	 */
	@Override
	public R getMaterialGroup(String materialFlag, String materialType) {
		List<MaterialGroup> materialGroups = baseMapper.getMaterialGroup(materialFlag, materialType);
		return R.data(materialGroups);
	}

}
